package me.lc.hammerheads.sprite.dwarfUnit;

import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.Animate;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.nodes.Animation;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.nodes.Animation.IAnimationCallback;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYRect;

import me.lc.hammerheads.core.GameManager;
import me.lc.hammerheads.core.GameSystem;
import me.lc.hammerheads.sprite.Dwarf;
import me.lc.hammerheads.util.AudioHelper;

/**
 * 炸弹人
 * 
 * @author Administrator
 * 
 */
public class DwarfBomb extends Dwarf implements IAnimationCallback {

	private Sprite exp;
	private boolean isBombed = false;
	private static float ItemWidth = GameSystem.tex_bombMan.getWidth() / 4.0f;
	private static float ItemHeight = GameSystem.tex_bombMan.getHeight();

	private static float ExpWidth = GameSystem.tex_bombMan_explosion.getWidth() / 6.0f;

	private WYRect frameAt(int x) {
		return WYRect.make(x * ItemWidth, 0, ItemWidth, ItemHeight);
	}

	private WYRect frameAtExp(int x) {
		return WYRect.make(x * ExpWidth, 0, ExpWidth, ItemHeight);
	}

	@Override
	protected void createDwart() {
		dwarf = Sprite.make(GameSystem.tex_bombMan, frameAt(0));
		exp = Sprite.make(GameSystem.tex_bombMan_explosion, frameAtExp(0));
		exp.setVisible(false);
	}

	@Override
	public int knock(int power) {
		knockOut();
		return 1;
	}

	@Override
	public synchronized void knockOut() {
		if (!isBombed) {
			if (isAlived()) {
				GameManager.instance.changeGameHeart(false);
				AudioHelper.dwarfExplosion();
				isBombed = true;
				explosion();
				super.knockOut();
			}
		}
	}

	@Override
	protected int getLifeTime() {
		return 7;
	}

	@Override
	protected int getLifeCount() {
		return 1;
	}

	@Override
	protected float getDwarfX() {
		return ItemWidth * 0.1162790697674419f;
	}

	@Override
	protected void doDwarfAction() {
		// 开始动画
		Animation animation = new Animation(0);
		animation
				.addFrame(0.1f, frameAt(0), frameAt(1), frameAt(2), frameAt(3));
		Animate a = (Animate) Animate.make(animation).autoRelease();
		Action repeat = (Action) RepeatForever.make(a).autoRelease();
		dwarf.runAction(repeat);

	}

	@Override
	protected float getDwarfWidth() {
		return ItemWidth;
	}

	@Override
	protected void drillhideOverProcess() {
		// 矮人钻回去不需要进行动作，需要覆盖基类中的方法
		// do nothing.
	}

	/**
	 * 爆炸
	 */
	private void explosion() {
		this.score.setColor(WYColor3B.make(0, 200, 0));
		this.addChild(exp, 2);
		exp.setScale(1.2f);
		exp.setVisible(true);
		Animation animation = new Animation(0);
		animation.addFrame(0.1f, frameAtExp(0), frameAtExp(1), frameAtExp(2),
				frameAtExp(3), frameAtExp(4), frameAtExp(5));
		animation.setCallback(this);
		Animate a = (Animate) Animate.make(animation).autoRelease();
		exp.runAction(a);
	}

	@Override
	public void onAnimationEnded(int arg0) {
		exp.setVisible(false);
	}

	@Override
	public void onAnimationFrameChanged(int arg0, int arg1) {
	}

	@Override
	protected int getKnockoutScore() {
		return -200;
	}

	@Override
	public void drillShow() {
		super.drillShow();
		AudioHelper.dwarfBombShow();
	}

}
